package br.senac.notafiscal.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.senac.notafiscal.entity.Cliente;
import br.senac.notafiscal.service.ClienteService;

@FacesConverter("clienteConverter")
public class ClienteConverter implements Converter {

	ClienteService clienteService = new ClienteService();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String id) {
		if (id != null && !id.isEmpty()) {
			return clienteService.getById(Long.parseLong(id));
		}

		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object cliente) {
		if (cliente instanceof Cliente) {
			return String.valueOf(((Cliente) cliente).getId());
		}
		return "";
	}

}
